<?php

namespace common\components;

use Yii;
use yii\helpers\Html;
use mdm\admin\components\Helper;
class ActionColumn extends \yii\grid\ActionColumn{

    public $filterAction = true;
    public $showHistory  = true;
    public $model_name;
    public $fk;
    public function init(){
        parent::init();
        if($this->showHistory && strpos($this->template, '{/history/history/index}') === false){
            $this->template .= ' {/history/history/index}';
        }
        if($this->filterAction){
            //$this->template = Helper::filterActionColumn($this->template);
        }
        $this->buttons['/history/history/index'] =  function ($url, $model, $key){
            $options = [
                'title'      => '日志',
                'aria-label' => '日志',
                'target'     => '_blank',
                'class'      => 'table-edit',
            ];
            return Html::a('日志', ['/history/history/index', 'HistoryRecordSearch' => ['model_name' => $this->model_name ?:get_class($model), 'fk' => $this->fk ? $model[$this->fk] : $model->getPrimaryKey()]], $options);
        };
    }

    protected function initDefaultButton($name, $iconName, $additionalOptions = [])
    {
        if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
            $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
                switch ($name) {
                    case 'view':
                        $title = Yii::t('yii', 'View');
                        break;
                    case 'update':
                        $title = Yii::t('yii', 'Update');
                        break;
                    case 'delete':
                        $title = Yii::t('yii', 'Delete');
                        break;
                    default:
                        $title = ucfirst($name);
                }
                $options = array_merge([
                    'title' => $title,
                    'aria-label' => $title,
                    'data-pjax' => '0',
                    'target' => '_blank',
                    'class' => $name == 'view' ? 'table-view' : 'table-edit'
                ], $additionalOptions, $this->buttonOptions);
                $icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
                return Html::a($icon, $url, $options);
            };
        }
    }
}